= "../models/model.pkl" model_path
Deploying models to Azure Managed Endpoints using Azure Machine Learning
Following this tutorial. Note that this is the most up-to-date method using the Python SDK v2 azure-ai-ml
, and all tutorials using azureml-core
(e.g. dp100 or this demo) are outdated, see this blog for more info.
Steps: 1. Setup Azure Machine Learning workspace and Azure Managed Online Endpoint. 2. Deploy model 3. Test model 4. Delete endpoint
Take model from:
= "azure-ml-deployment-methods"
workspace = "azure-cognitive-services-accelerator" resource_group
1. Setup
1.1 Setup Azure Machine Learning
First create a new Azure Machine Learning workspace in the Azure portal.
= !az account show --query id subscription_id
Connect to Azure ML workspace
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
= MLClient(DefaultAzureCredential(), subscription_id[0][1:-1], resource_group, workspace) client
1.2 Setup Azure Managed Online Endpoint
from azure.ai.ml.entities import ManagedOnlineEndpoint
= ManagedOnlineEndpoint(
endpoint ="sample-model-endpoint",
name="A sample ML model deployment to Managed Endpoints",
description="key",
auth_mode={'area': 'diabetes', 'type': 'regression'},
tags )
client.online_endpoints.begin_create_or_update(endpoint).result()
2. Deploy model to endpoint
from azure.ai.ml.entities import ManagedOnlineDeployment, Model, Environment, CodeConfiguration
Configure deployment: select environment and model. Here we use a base Ubuntu image and add conda dependencies.
!mkdir files
%%writefile files/inference.py
import json, joblib, os
import numpy as np
def init():
global model
= joblib.load(os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'model.pkl'))
model
def run(raw_data):
= model.predict(np.array(json.loads(raw_data)['data'])).tolist()
predictions return json.dumps(predictions)
%%writefile files/conda.yml
-env
name: model
channels:- defaults
dependencies:- python=3.7
- pip
- pip:
- scikit-learn
- joblib
- azureml-defaults
- inference-schema[numpy-support]
= Model(path="../models/model.pkl")
model
= env = Environment(
env ="files/conda.yml",
conda_file="mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04:latest",
image
)
= CodeConfiguration(code="files", scoring_script="inference.py") code_config
Create deployment and connect to endpoint
= ManagedOnlineDeployment(
deployment ="blue",
name="sample-model-endpoint",
endpoint_name=model,
model=env,
environment=code_config,
code_configuration="Standard_DS2_v2",
instance_type=1,
instance_count )
Deploy. This creates a deployment which you can view in Azure Machine Learning Studio
/Endpoints
/<name>
client.online_deployments.begin_create_or_update(deployment).result()
= {"blue": 100}
endpoint.traffic client.begin_create_or_update(endpoint)
3. Test endpoint
Grab API key from Azure Machine Learning Studio
/Endpoints
/<name>
/Consume
= client.online_endpoints.get(name="sample-model-endpoint").scoring_uri
uri = "" key
import requests
import json
import numpy as np
= json.dumps({
input_payload 'data': np.load("../data/diabetes.npz")["X_test"][:2].tolist(),
})
={
requests.post(uri, input_payload, headers'Content-Type':'application/json',
'Authorization':('Bearer '+key)
}).json()
'[230.86996560914233, 241.27351292981544]'
4. Delete
="sample-model-endpoint") client.online_endpoints.begin_delete(name
!rm -r files
.....................................